/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tt.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author andi
 */
@Entity
@Table(name = "exam")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Exam.findAll", query = "SELECT e FROM Exam e"),
    @NamedQuery(name = "Exam.findByIdexam", query = "SELECT e FROM Exam e WHERE e.idexam = :idexam"),
    @NamedQuery(name = "Exam.findByExamdate", query = "SELECT e FROM Exam e WHERE e.examdate = :examdate"),
    @NamedQuery(name = "Exam.findByStartofexam", query = "SELECT e FROM Exam e WHERE e.startofexam = :startofexam"),
    @NamedQuery(name = "Exam.findByExamroom", query = "SELECT e FROM Exam e WHERE e.examroom = :examroom")})
public class Exam implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idexam")
    private Integer idexam;
    @Column(name = "examdate")
    @Temporal(TemporalType.DATE)
    private Date examdate;
    @Column(name = "startofexam")
    @Temporal(TemporalType.TIME)
    private Date startofexam;
    @Size(max = 45)
    @Column(name = "examroom")
    private String examroom;
    @Lob
    @Size(max = 65535)
    @Column(name = "infoexam")
    private String infoexam;
    @OneToMany(mappedBy = "exam")
    private Collection<Lecture> lectureCollection;
    @JoinColumn(name = "secondexaminer", referencedColumnName = "idlecturer")
    @ManyToOne
    private Lecturer secondexaminer;
    @JoinColumn(name = "firstexaminer", referencedColumnName = "idlecturer")
    @ManyToOne
    private Lecturer firstexaminer;
    @Column(name = "timestampexam")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestampexam;

    public Exam() {
    }

    public Exam(Integer idexam) {
        this.idexam = idexam;
    }

    public Integer getIdexam() {
        return idexam;
    }

    public Date getTimestampexam() {
        return timestampexam;
    }

    public void setTimestampexam(Date timestampexam) {
        this.timestampexam = timestampexam;
    }

    public void setIdexam(Integer idexam) {
        this.idexam = idexam;
    }

    public Date getExamdate() {
        return examdate;
    }

    public String getExamdateShort() {
        String dateDayLong = examdate.toString();
        String dateDay = examdate.toString();
        String dateMonth = examdate.toString();
        String dateYear = examdate.toString();
        dateDayLong = dateDayLong.substring(0, 3);
        dateDay = dateDay.substring(8, 10);
        dateMonth = dateMonth.substring(4, 7);
        dateYear = dateYear.substring(25, 29);

        /*
         * Wochentag auf Deutsch und ausgeschrieben darstellen
         */
        if (dateDayLong.compareTo("Mon") == 0) {
            dateDayLong = "Montag, ";
        } else if (dateDayLong.compareTo("Tue") == 0) {
            dateDayLong = "Dienstag, ";
        } else if (dateDayLong.compareTo("Wed") == 0) {
            dateDayLong = "Mittwoch, ";
        } else if (dateDayLong.compareTo("Thu") == 0) {
            dateDayLong = "Donnerstag, ";
        } else if (dateDayLong.compareTo("Fri") == 0) {
            dateDayLong = "Freitag, ";
        } else if (dateDayLong.compareTo("Sat") == 0) {
            dateDayLong = "Samstag, ";
        } else if (dateDayLong.compareTo("Sun") == 0) {
            dateDayLong = "Sonntag, ";
        }

        /*
         * Monat Aug in Dezimalzahl 08 umwandeln
         */
        if (dateMonth.compareTo("Jan") == 0) {
            dateMonth = ".01.";
        } else if (dateMonth.compareTo("Feb") == 0) {
            dateMonth = ".02.";
        } else if (dateMonth.compareTo("Mar") == 0) {
            dateMonth = ".03.";
        } else if (dateMonth.compareTo("Apr") == 0) {
            dateMonth = ".04.";
        } else if (dateMonth.compareTo("May") == 0) {
            dateMonth = ".05.";
        } else if (dateMonth.compareTo("Jun") == 0) {
            dateMonth = ".06.";
        } else if (dateMonth.compareTo("Jul") == 0) {
            dateMonth = ".07.";
        } else if (dateMonth.compareTo("Aug") == 0) {
            dateMonth = ".08.";
        } else if (dateMonth.compareTo("Sep") == 0) {
            dateMonth = ".09.";
        } else if (dateMonth.compareTo("Oct") == 0) {
            dateMonth = ".10.";
        } else if (dateMonth.compareTo("Nov") == 0) {
            dateMonth = ".11.";
        } else if (dateMonth.compareTo("Dec") == 0) {
            dateMonth = ".12.";
        }

        return dateDayLong + dateDay + dateMonth + dateYear;
        //Thu Jan 01 11:45:00 CET 1970 => Donnerstag, 01.01.1970

    }//end method

    
     public String getTimestampexamShort() {
        String dateDayLong = timestampexam.toString();
        String dateDay = timestampexam.toString();
        String dateMonth = timestampexam.toString();
        String dateYear = timestampexam.toString();
        dateDayLong = dateDayLong.substring(0, 3);
        dateDay = dateDay.substring(8, 10);
        dateMonth = dateMonth.substring(4, 7);
        dateYear = dateYear.substring(25, 29);

        /*
         * Wochentag auf Deutsch und ausgeschrieben darstellen
         */
        if (dateDayLong.compareTo("Mon") == 0) {
            dateDayLong = "Montag, ";
        } else if (dateDayLong.compareTo("Tue") == 0) {
            dateDayLong = "Dienstag, ";
        } else if (dateDayLong.compareTo("Wed") == 0) {
            dateDayLong = "Mittwoch, ";
        } else if (dateDayLong.compareTo("Thu") == 0) {
            dateDayLong = "Donnerstag, ";
        } else if (dateDayLong.compareTo("Fri") == 0) {
            dateDayLong = "Freitag, ";
        } else if (dateDayLong.compareTo("Sat") == 0) {
            dateDayLong = "Samstag, ";
        } else if (dateDayLong.compareTo("Sun") == 0) {
            dateDayLong = "Sonntag, ";
        }

        /*
         * Monat Aug in Dezimalzahl 08 umwandeln
         */
        if (dateMonth.compareTo("Jan") == 0) {
            dateMonth = ".01.";
        } else if (dateMonth.compareTo("Feb") == 0) {
            dateMonth = ".02.";
        } else if (dateMonth.compareTo("Mar") == 0) {
            dateMonth = ".03.";
        } else if (dateMonth.compareTo("Apr") == 0) {
            dateMonth = ".04.";
        } else if (dateMonth.compareTo("May") == 0) {
            dateMonth = ".05.";
        } else if (dateMonth.compareTo("Jun") == 0) {
            dateMonth = ".06.";
        } else if (dateMonth.compareTo("Jul") == 0) {
            dateMonth = ".07.";
        } else if (dateMonth.compareTo("Aug") == 0) {
            dateMonth = ".08.";
        } else if (dateMonth.compareTo("Sep") == 0) {
            dateMonth = ".09.";
        } else if (dateMonth.compareTo("Oct") == 0) {
            dateMonth = ".10.";
        } else if (dateMonth.compareTo("Nov") == 0) {
            dateMonth = ".11.";
        } else if (dateMonth.compareTo("Dec") == 0) {
            dateMonth = ".12.";
        }
  return dateDay + dateMonth + dateYear;
      //  return dateDayLong + dateDay + dateMonth + dateYear;
        //Thu Jan 01 11:45:00 CET 1970 => Donnerstag, 01.01.1970

    }//end method
    
    public void setExamdate(Date examdate) {
        this.examdate = examdate;
    }

    public String getStarttimeShort() {
        String time = startofexam.toString();
        time = time.substring(11, 16) + " Uhr";
        return time;
        //Thu Jan 01 11:45:00 CET 1970 => 11:45 Uhr

    }//end method

    public Date getStartofexam() {
        return startofexam;
    }

    public void setStartofexam(Date startofexam) {
        this.startofexam = startofexam;
    }

    public String getExamroom() {
        return examroom;
    }

    public void setExamroom(String examroom) {
        this.examroom = examroom;
    }

    public String getInfoexam() {
        return infoexam;
    }

    public void setInfoexam(String infoexam) {
        this.infoexam = infoexam;
    }

    @XmlTransient
    public Collection<Lecture> getLectureCollection() {
        return lectureCollection;
    }

    public void setLectureCollection(Collection<Lecture> lectureCollection) {
        this.lectureCollection = lectureCollection;
    }

    public Lecturer getSecondexaminer() {
        return secondexaminer;
    }

    public void setSecondexaminer(Lecturer secondexaminer) {
        this.secondexaminer = secondexaminer;
    }

    public Lecturer getFirstexaminer() {
        return firstexaminer;
    }

    public void setFirstexaminer(Lecturer firstexaminer) {
        this.firstexaminer = firstexaminer;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idexam != null ? idexam.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Exam)) {
            return false;
        }
        Exam other = (Exam) object;
        if ((this.idexam == null && other.idexam != null) || (this.idexam != null && !this.idexam.equals(other.idexam))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "tt.model.Exam[ idexam=" + idexam + " ]";
    }
    
    public boolean isUpdate()
   {
      long isNew;
      Date lastDate = getTimestampexam();
      Date curDate = new Date();
      long lastDate_long = lastDate.getTime();
      long curDate_long = curDate.getTime();
      isNew = curDate.getTime() - lastDate.getTime();

      if (isNew < 0)
      {
         isNew = isNew * (-1);
      }//endif

      //Wert "12096e5" = 2 Wochen in Millisekunden
      if (isNew <= 12096e5)
      {
         return true;
      } else
      {
         return false;
      }//endif
   }//endmethod
    
    
    
}
